package com.zhang.teachingcommunity_spring.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.teachingcommunity_spring.mapper.PostMapper;
import com.zhang.teachingcommunity_spring.mapper.SubjectMapper;
import com.zhang.teachingcommunity_spring.mapper.UserMapper;
import com.zhang.teachingcommunity_spring.mapper.entity.Post;
import com.zhang.teachingcommunity_spring.mapper.entity.Subject;
import com.zhang.teachingcommunity_spring.mapper.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private UserMapper userMapper;

    public IPage<Post> getPostList(Post post, Page Ipage,Integer userId) {
        QueryWrapper<Post> userQueryWrapper = new QueryWrapper<>();
        if(post.getSchoolId() != null && !(post.getSchoolId().equals(""))) {
            userQueryWrapper.eq("school_id", post.getSchoolId());
        }
        if(post.getTag() != null && !(post.getTag().equals(""))) {
            userQueryWrapper.eq("tag", post.getTag());
        }
        if(userId != null && !(userId.equals(""))) {
            userQueryWrapper.eq("user_id", userId);
        }

        IPage<Post> page = new Page<>(Ipage.getCurrent(),Ipage.getSize());
        IPage<Post> userIPage = postMapper.selectPage(page, userQueryWrapper);
        userIPage.getRecords().forEach(item-> {
            String tag = item.getTag();
            Integer Id = item.getUserId();
            User user = userMapper.selectById(Id);
            Subject subject = subjectMapper.selectById(tag);
            item.setSubject(subject);
            item.setUser(user);
        });
        return userIPage;
    }

}
